package cn.keduox.controller;

import cn.keduox.entity.HotBook;
import cn.keduox.entity.HotBookType;
import cn.keduox.entity.PageBean;
import cn.keduox.service.impl.HotBookTypeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * User: Yongman
 * Date: 2018/1/2
 * 畅销榜页面控制层
 */
@WebServlet("/hotBook.do")
public class HotBookController extends HttpServlet{
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        获取当前页数
        String currentPage = request.getParameter("currentPage");
//        type，类型编码1，2，。。。
        String type = request.getParameter("type");
//        创建畅销榜服务层对象
        HotBookTypeServiceImpl hotBookTypeService = new HotBookTypeServiceImpl();
//        调用查询方法
        List<HotBookType> hotBookTypes = hotBookTypeService.query();
//        设置分页参数
        PageBean pageBean = new PageBean();
        if (currentPage == null || "".equals(currentPage)){
            pageBean.setCurrentPage(1);
        }else {
            pageBean.setCurrentPage(Integer.parseInt(currentPage));
        }

//        根据类型查
        List<HotBook> hotBooks = hotBookTypeService.queryByType(type,pageBean);
        if (hotBooks!=null && hotBooks.size()>0){
            pageBean = hotBooks.get(0).getPageBean();
        }

//        设置request域
        request.setAttribute("hotBookTypes", hotBookTypes);
        request.setAttribute("hotBooks", hotBooks);
        request.setAttribute("type", type);
        request.setAttribute("pageBean",pageBean);

//        内部跳转到畅销榜页面
        request.getRequestDispatcher("/WEB-INF/pages/hot_book.jsp").forward(request,response);

    }
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

}
